package gods.game.characters;

import gods.base.Renderable;
import gods.game.characters.weapons.Projectile;

import java.awt.Graphics2D;
import java.util.*;

public class HostileWeaponSet implements Renderable 
{
	public void render(Graphics2D g) 
	{
		for (Projectile p : m_list)
		{
			p.render(g);
		}
		
	}

	private boolean m_within_update = false;
	private LinkedList<Projectile> m_list = new LinkedList<Projectile>();
	private LinkedList<Projectile> m_delayed_list = new LinkedList<Projectile>();
	
	public void update(long elapsed_time) 
	{
		m_within_update = true;
		
		if (!m_delayed_list.isEmpty())
		{
			m_list.addAll(m_delayed_list);
			m_delayed_list.clear();
		}
		ListIterator<Projectile> it = m_list.listIterator();
		
		while (it.hasNext())
		{
			Projectile p = it.next();
			
			p.update(elapsed_time);
			
			if (p.get_state() == Projectile.State.DEAD)
			{
				it.remove();
			}
		}
		
		m_within_update = false;
		
	}

	
	public HostileWeaponSet()
	{
		
	}
	// optimization: when weapon fired from hostile, direct add to list
	// if weapon generated by another "mother" weapon, delayed add (to avoid concurrent
	// modification)

	public void add(Projectile p)
	{
		if (m_within_update)
		{
			m_delayed_list.add(p);
		}
		else
		{
			m_list.add(p);
		}
	}
}
